package com.aimuceliang.aimuceliang.ui;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.aimuceliang.aimuceliang.dev.R;
import com.aimuceliang.aimuceliang.utils.StringUtil;

import butterknife.BindView;

/**
 * Created by 80470 on 2017/8/2.
 * webView的公共类
 */

public class WebViewActivity extends BaseActivity {

    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.web_progress_bar)
    ProgressBar myProgressBar;

    @Override
    protected int initContentView() {
        return R.layout.activity_web_view;
    }

    public static void startActivity(Context mContext, String title, String apiPath) {
        Intent intent = new Intent(mContext, WebViewActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("apiPath", apiPath);
        mContext.startActivity(intent);
    }

    @Override
    protected void initUIAndListener() {
        String title = getIntent().getStringExtra("title");
        String apiPath = getIntent().getStringExtra("apiPath");
        String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
        webView.getSettings().setGeolocationDatabasePath(dir);
        webView.getSettings().setGeolocationEnabled(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setDatabaseEnabled(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);// 允许js弹出窗口
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); // 设置
        webView.getSettings().setDomStorageEnabled(true);
        String ua = webView.getSettings().getUserAgentString();
        webView.getSettings().setUserAgentString(ua + "; Scn Browser");
        webView.clearHistory();
        webView.clearCache(true);
        webView.clearFormData();
        if (StringUtil.checkStr(title)) {
            setTitle(title);
        }
        if (StringUtil.checkStr(apiPath)) {
            webView.loadUrl(apiPath);
        }
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    myProgressBar.setVisibility(View.GONE);
                } else {
                    if (View.GONE == myProgressBar.getVisibility()) {
                        myProgressBar.setVisibility(View.VISIBLE);
                    }
                    myProgressBar.setProgress(newProgress);
                }
            }
        });
    }

    @Override
    protected void initData() {

    }
}
